import { Form, Input, Button, Checkbox, message } from 'antd';
import userModel from '../model/userModel';
import { useHistory } from "react-router-dom"

const Demo = () => {

    let history = useHistory();

    const onFinish = async (info: any) => {
        let { username, userpwd } = info;
        console.log(username,userpwd)
        let { data } = await userModel.login({ username, userpwd });
        // console.log(data)

        if (data.code) {
            let token  = data.token;
            window.localStorage.setItem("token",token);
            history.push("/"+data.path);

        } else {
            message.info("用户名和密码不匹配");
        }

        // if(list.length){
        //     alert("登陆成功");
        // }else{

        //     message.info("用户名和密码不匹配");
        // }

        // console.log('Success:', values);
    };

    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };

    return (
        <div className='login'>
            <Form
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="username"
                    rules={[
                        { required: true, message: '用户名不能为空' },
                        {
                            pattern: /^[a-z][0-9a-z]{3,9}$/i, message: "只能输入数字字母组成长度3到10位不能数字开头"
                        }
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="userpwd"
                    rules={[{ required: true, message: '密码不能为空' }, {
                        pattern: /^[0-9a-z]{3,9}$/i, message: "只能输入数字字母组成长度4到10位"
                    }]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 8, span: 16 }}>
                    <Checkbox>Remember me</Checkbox>
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
};

export default Demo;
